<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\v1\controller;

use api\v1\model\Category;
use api\v1\model\Jifenorder;
use api\v1\model\JifenProduct;
use api\v1\model\User;
use cmf\controller\RestBaseController;
use cmf\controller\RestUserBaseController;
use think\db\Query;
use think\facade\Db;

/**
 * 首页接口
 * Class IndexController
 * @package api\v1\controller
 */
class JifenmallController extends RestUserBaseController
{
    public function productList() {

        $data = [];
        $data['list'] = Category::with(['jfproducts'=>function($query){
            $query->where('status',1)->order("sort desc");
        }])->where("type",2)->order("sort desc")->select();

        $this->success("ok",$data);
    }
    public function getProduct() {
        $product = JifenProduct::find($this->request->param('id'));
        $this->success("ok",['product'=>$product]);
    }
    public function orderList() {
        $list = Jifenorder::with("product")
            ->where("user_id",$this->userId)
            ->order("id",'desc')
            ->paginate();
        $this->success("ok",['list'=>$list->items(),'total'=>$list->total()]);
    }
    public function getOrder() {
        $order = Jifenorder::with("product")
            ->where("user_id",$this->userId)
            ->where("id",$this->request->param('id'))
            ->find();
        $this->success('ok',$order);
    }
    public function addOrder() {
        $data = $this->request->post();
        $product = JifenProduct::find($data['id']);
        if(!$product) {
            $this->error("商品不存在！");
        }
        $user = User::find($this->userId);
        if($product->price>$user->score) {
            $this->error("积分不足！");
        }
        Db::startTrans();
        try {
            unset($data['id']);
           $order = Jifenorder::create([
                "user_id"=>$user->id,
                "product_id"=>$product->id,
                "num"=>1,
                "price"=>$product->price,
                "total_price"=>$product->price,
                "username"=>$data['name'],
                "phone"=>$data['phone'],
                "address"=>$data["address"]
            ]);
            $user->address = $data;
            $user->score = $user->score - $order->total_price;
            $user->save();
            $product->sales = $product->sales + 1;
            $product->stock = $product->stock - 1;
            $product->save();

            User::score_change($user->id,-$order->total_price,2,"兑换商品:(".$product->id . ")".$product->title);
            Db::commit();
        }
        catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success("兑换成功！");
    }
}
